#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char 
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};
uchar data tab[2];
uchar ws[2]={0,0};
uchar code table2[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7};
uchar code tablewei[]={0x0e,0x0d};

sbit dula=P1^5;
sbit dawr=P2^6;//DA写数据段 
sbit dacs=P2^7;//DA始能段

uchar num1,f,f1,f2,f3,f4,f5,f6,keynum;
//f为数字键标志位;f1为DA转换标志位;f2,f4分别为+,-0.1标志位;f3为清零标志位
 void delay(uint z)//延时子程序
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}
void display(uint number)//显示LED
{  
     
       uchar i,j;
       tab[0]=number%10;
       tab[1]=number/10;
        for(i=0,j=0;i<2;i++,j++)
     {   
         
         dula=1;
         P2=tablewei[j];
         if(i==0)
         P0=table[tab[i]];
         else
         P0=table2[tab[i]];
         dula=0;
         delay(5);
     }

}
 keyscan()//键盘扫描
{
   uchar num,tt; 
   P1=0xfe;
   tt=P1;
   tt=tt&0xf0;
   while(tt!=0xf0)  // 检查按键是否被摁下
   {
	      delay(50);
	      tt=P1;
	      tt=tt&0xf0;
		     while(tt!=0xf0)  // 检查按键是否被摁下
		    {    
				 tt=P1; 
                  switch(tt)
				 {
				         case 0xee: num=7,f=1;
				    break;
				         case 0xde: num=8;f=1;
				    break;
				         case 0xbe: num=9;f=1;
				    break;
				         case 0x7e: num='0';f3=1;//清零按键    
				    break;
                    default:break;				         
		        }
               while(tt!=0xf0)
                {
                   tt=P1;
                   tt=tt&0xf0;
                }

             }
       
   } 
   P1=0xfd;
   tt=P1;
   tt=tt&0xf0;
   while(tt!=0xf0)  // 检查按键是否被摁下
   {
	      delay(50);
	      tt=P1;
	      tt=tt&0xf0;
		     while(tt!=0xf0)  // 检查按键是否被摁下
		    {
				 tt=P1; 
                  switch(tt)
				 {
				         case 0xed: num=4;f=1;
				    break;
				         case 0xdd: num=5;f=1;
				    break;
				         case 0xbd: num=6;f=1;
				    break;
				         case 0x7d: num=14;f4=1;//减0.1按键
				    break;
                     default:break;
		         }
              while(tt!=0xf0)
                {
                   tt=P1;
                   tt=tt&0xf0;
                }               

            }
       
   } 
   P1=0xfb;
   tt=P1;
   tt=tt&0xf0;
   while(tt!=0xf0)  // 检查按键是否被摁下
   {
	      delay(50);
	      tt=P1;
	      tt=tt&0xf0;
		     while(tt!=0xf0)  // 检查按键是否被摁下
		    {
				 tt=P1; 
                  switch(tt)
				 {
				         case 0xeb: num=1;f=1;
				    break;
				         case 0xdb: num=2;f=1;
				    break;
				         case 0xbb: num=3;f=1;
				    break;
				         case 0x7b: num='+';f2=1;//加0.1按键
				    break;
	                default:break;
		        }
              while(tt!=0xf0)
                {
                   tt=P1;
                   tt=tt&0xf0;
                }
 
             }
       
   } 
   P1=0xf7;
   tt=P1;
   tt=tt&0xf0;
   while(tt!=0xf0)  // 检查按键是否被摁下
   {
	      delay(50);
	      tt=P1;
	      tt=tt&0xf0;
		     while(tt!=0xf0)  // 检查按键是否被摁下
		    {
				 tt=P1; 
                  switch(tt)
				 {
				         case 0xe7: num=0;f=1;
				    break;
				         case 0xd7: num=10;f1=1;//确认DA转换按键
				    break;
				         case 0xb7: num=11;f5=1;
				    break;
				         case 0x77: num=12;f6=1;
				    break;
                    default:break;

		        }
              while(tt!=0xf0)
                {
                   tt=P1;
                   tt=tt&0xf0;
                }
               
           }
       
   } 
   return(num);
}

void jishu()//预置电压值
{       
       keynum=keyscan();
       if(f==1)
        {  
           f=0;
           num1=num1*10+keynum;
        }
}

void change()//DA转换
{
    if(f1==1)
    {
       P3=(uchar)(256*num1/100);
       delay(10);
    }
}

void up_down_clear()//加减0.1和清零
{
    if(f2==1)
    { 
      f2=0;
      num1++;
    }
    if(f4==1)
    {
     f4=0;
     num1--;
    }
    if(f3==1)
    {
      f3=0;
      num1=0;
    }
}
void wave() //波形发生器
{
     int i;
     if (f5 == 1) //方波
     {

          while (1)
          {
               display(num1);
               P3 = (uchar)(256 * num1 / 100);
               delay(10);
               P3 = 0x0f;
               delay(5);
          }
     }
     if (f6 == 1) //三角波
     {
          while (1)
          {

               for (i = 0; i <= (uchar)(256 * num1 / 100); i++)
               {
                    P3 = i;
                    delay(2);
               }
               for (i = (uchar)(256 * num1 / 100); i >= 0; i--)
               {
                    P3 = i;
                    delay(2);
               }
          }
     }
}
void main()
{
     dawr = 0;
     dacs = 0;
     P0 = 0;
     while (1)
     {

          jishu();
          up_down_clear();
          wave();
          display(num1);
          change();
     }
}